{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2974483b",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 关于麦克风包安装\n",
    "\n",
    "## 优化ASR(自动语音识别)机器人\n",
    "\n",
    "## 连接图灵机器人(不用自己的字典)\n",
    "\n",
    "## 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "affacbb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: SpeechRecognition in d:\\conda\\anaconda\\lib\\site-packages (3.8.1)\n"
     ]
    }
   ],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4303bd3e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: PyAudio in d:\\conda\\anaconda\\lib\\site-packages (0.2.11)\n"
     ]
    }
   ],
   "source": [
    "!pip install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2b3ce678",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pydub in d:\\conda\\anaconda\\lib\\site-packages (0.25.1)\n"
     ]
    }
   ],
   "source": [
    "!pip install pydub"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30e106da",
   "metadata": {},
   "source": [
    "# 优化ASR(自动语音识别)机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9d2cd343",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "eae0a23f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 百度密钥\n",
    "API_KEY = 'WYlCIGp63cM3HnKw1MG9BETt'\n",
    "SECRET_KEY = 'qC7X6vjG5pXHPhi7wAWGK9HCpuQUtW6U'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "40506c91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.51087dc1a51debee7bb9caa6c0d93345.2592000.1656588396.282335-26255870'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "tu_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2f7d7e6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "#通过调用电脑麦克风进行音频的获取\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7b2edbff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.548061\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7103860812209383465\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"\"],\"sn\":\"952036669581653996485\"}\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(tu_token,\"1.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d1f9439",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = eval(asr(token = tu_token, AUDIO_FILE = '1.wav'))['result'][0]\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e01dd446",
   "metadata": {},
   "source": [
    "# 连接图灵机器人\n",
    "\n",
    "* API V2.0是基于图灵机器人平台语义理解、深度学习等核心技术，为广大开发者和企业提供的在线服务和开发接口。\n",
    "\n",
    "* 接口地址：\n",
    "> http://openapi.turingapi.com/openapi/api/v2\n",
    "\n",
    "* 请求方式:\n",
    "> POST\n",
    "\n",
    "* 请求参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93217175",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f579684e",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_key = \"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd3aac09",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_url = 'http://openapi.turingapi.com/openapi/api/v2'\n",
    "params={\n",
    "        \"reqType\":0,\n",
    "        \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": \"苹果用英语怎么说\"\n",
    "            },\n",
    "        },\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": TL_key,\n",
    "            \"userId\": \"0000001\"\n",
    "        }\n",
    "    }\n",
    "# print(type(params),params)\n",
    "# print(type(json.dumps(params)),json.dumps(params))\n",
    "res = requests.post(TL_url,data = json.dumps(params))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d4326faf",
   "metadata": {},
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50fcb1ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "res.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e339b8f8",
   "metadata": {},
   "source": [
    "# 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50aa0920",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tuling import TuLing "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba67b261",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tuling import TuLing \n",
    "TuLing(TL_key,'你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47ea575f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c558afd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import tts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0e2d043",
   "metadata": {},
   "outputs": [],
   "source": [
    "tts(tu_token,\"这里是百度语音合成接口\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11e833f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "song = AudioSegment.from_wav('result.wav')\n",
    "\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1e8bbd7",
   "metadata": {},
   "source": [
    "\n",
    "# 自动对话机器人\n",
    "* 目标：我说一句话，电脑回复我文本信息(TULING_API)\n",
    ">1.我们讲话，录制音频，存放在＇1，wav＇测试文件中   \n",
    ">2.语音识别：百度asr接口实现语音识别功能，将音频转换未文本    \n",
    ">3.接入图灵机器人，实现文本回复    \n",
    ">4.语音合成：百度tts接口实现语音合成功能，将回复的文本转换成音频     \n",
    ">5.通过python play模块进行播放  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c426f8e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition#录制音频模块\n",
    "from baidu_speech import fetch_token,asr,tts#百度语音系统模块\n",
    "from tuling import TuLing #图灵机器人\n",
    "from pydub import AudioSegment#播放音频模块\n",
    "from pydub.playback import play#播放音频模块\n",
    "\n",
    "# 准备音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "#通过调用电脑麦克风进行音频的获取\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "# 2. 调用百度\n",
    "tu_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "result = eval(asr(token = tu_token, AUDIO_FILE = '1.wav'))['result'][0]\n",
    "\n",
    "# 3. 调用图灵机器，实现文本回复\n",
    "TL_result = TuLing(TL_key,result)\n",
    "print(TL_result)\n",
    "# 4. 语音合成：百度tts接口实现语音合成功能，将回复的文本转换成音频\n",
    "tts(tu_token,TL_result)\n",
    "# 5.通过python play模块进行播放\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebf1f093",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
